package com.bianguo.android.apushdemo.view.activity;

import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.utils.MyLog;

import butterknife.BindView;
import butterknife.OnClick;

public class NewsDetailActivity extends BaseActivity {

    @BindView(R.id.title_title)
    TextView titleTitleTv;
    @BindView(R.id.news_detail_web)
    WebView mWebView;

    public final String NEWS_DETAIL_BASE_URL = "http://demo.bianguo.com.cn/zhuzhenyu/scnews/index.php/Home/Index/news?Nid=8";
    private WebSettings webSettings;

    @Override
    public int layoutId() {
        return R.layout.activity_news_detail;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        titleTitleTv.setText("网页");
        webSettings = mWebView.getSettings();
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        mWebView.loadUrl(NEWS_DETAIL_BASE_URL);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(final WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onReceivedTitle(WebView view, String title) {
                MyLog.mLog("title = " + title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                MyLog.mLog("newProgress = " + newProgress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

                return super.onJsAlert(view, url, message, result);
            }
        });
    }

    @OnClick({R.id.title_left_ib})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.title_left_ib:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }
}
